<?php
namespace App\Admin\Exporter\Order;

use App\Models\AdminUserModel;
use App\Models\yb\GoodsModel;
use App\Models\yb\GoodsOrderDetailModel;
use App\Models\yb\GoodsTypeModel;
use App\Models\yb\UserModel;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
use Maatwebsite\Excel\Concerns\WithHeadings;

class OrderExpoter extends ExcelExporter implements WithMapping, WithStrictNullComparison, WithHeadings
{

    protected $fileName = "订单列表.xlsx";

    public function headings(): array
    {

        if (!empty($this->columns)) {

            return array_values(array_unique($this->columns));
        }

        return parent::headings();
    }
    // 设置需要从库中检索出的字段
    protected $columns = [
        'order_sn' => '订单编号',
        'user_id' => '下单人昵称',
        'openid' => '下单手机号',
        'network_id' => '网点名称',
        'total_price' => '订单金额',
        'amount' => '实付金额',
        'status' => '订单状态',
        'created_at' => '创建时间',
    ];
    // 设置映射字段
    public function map($row): array
    {
        $user_data = UserModel::where(['id'=>$row->user_id])->first();
        $goods_data = GoodsModel::where(['id'=>$row->goods_id])->first();
        $network_data = AdminUserModel::where(['id'=>$row->network_id])->first();
        $status="";
        if($row->status == 0){
            $status = "待支付";
        }elseif($row->status == 1){
            $status = "待接单";
        }elseif($row->status == 2){
            $status = "待配送";
        }elseif($row->status == 3){
            $status = "已送达";
        }elseif($row->status == 4){
            $status = "已完成";
        }
        return [
            isset($row->order_sn)?$row->order_sn:"",
            isset($user_data['nickname'])?$user_data['nickname']:"",
            isset($user_data['phone'])?$user_data['phone']:"",
            isset($user_data['name'])?$user_data['name']:"",
            isset($row->total_price)?$row->total_price:"",
            isset($row->amount)?$row->amount:"",
            // GoodsTypeModel::selectValue('id',$goods_data['goods_type']),
            $status,
            $row->created_at,
        ];
    }
}
